package com.c.ctgproject.ui.collect

import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.c.ctgproject.adapter.guang.PartCanHandlerAdapter
import com.c.ctgproject.base.BaseActivityV2
import com.c.ctgproject.databinding.ActivityPartOutListBinding
import com.c.ctgproject.entity.guang.PartOutListEntity
import com.c.ctgproject.http.ApiMethods
import com.c.ctgproject.http.MyObserverHandlerLoading
import com.c.ctgproject.utils.*
import org.jetbrains.anko.startActivity

/**
 *作者:LiBW
 *创建日期: 2021/9/17
 *描述: 零部件出库 页面
 */
class PartOutListActivity : BaseActivityV2() {
    private val mBinding by lazy { ActivityPartOutListBinding.inflate(layoutInflater) }
    override fun getLayout() = mBinding.root
    private var page = 1
    private var search: String = ""
    private val mList = ArrayList<PartOutListEntity.DataBean.ListBean>()
    private val mAdapter by lazy { PartCanHandlerAdapter(this, mList,null) }

    override fun onResume() {
        super.onResume()
        page = 1
        getData()
    }

    override fun event() {

    }

    override fun click() {
        mBinding.mTabLayout.visibility = View.GONE
        mBinding.mTabLayout.handlerSelectReturnText {
            when (it) {
                "未出库" -> {
                    mBinding.mBottomRL.visibility = View.VISIBLE
                    searchMine = false
                    page = 1
                    getData()
                }
                "我的" -> {
                    mBinding.mBottomRL.visibility = View.GONE
                    searchMine = true
                    page = 1
                    getData()
                }
            }
        }
        mBinding.mCarRV.adapter = mAdapter
        mBinding.mCarRV.layoutManager = LinearLayoutManager(this)

        mBinding.mSearchET.handlerText {
            if (it.isEmpty() || it == "") {
                search = ""
                page = 1
                getData()
            }
        }
        mBinding.mSearchTV.setOnClickListener {
            search = mBinding.mSearchET.text.handlerNull()
            page = 1
            getData()
        }
        mBinding.mRefresh.setOnLoadMoreListener {
            page++
            getData()
        }

        mBinding.mRefresh.setOnRefreshListener {
            page = 1
            getData()
        }
        //去拆解
        mBinding.mConfirmBTN.setOnClickListener {
            val selectList = ArrayList<PartOutListEntity.DataBean.ListBean>()
            mList.forEach {
                if (it.isSelect) selectList.add(it)
            }
            if (selectList.isEmpty()) {
                showToast("请至少选择一个进行出库")
            } else {
                //去出库
                startActivity<PartOutSubmitActivity>(MyConstants.DATA to selectList)
//                startActivity<BatteryOutSubmitActivity>(MyConstants.DATA to selectList)
            }
        }
    }

    private var searchMine = false
    private fun getData() {
        if (searchMine) {
            ApiMethods.apiSubscribe(ApiMethods.retrofit.myPartOut(search, page), MyObserverHandlerLoading(this, loading, mBinding.mRefresh) {
                MyLog.d("我的零部件出库回调:$it")
                val data = getGson().fromJson(it, PartOutListEntity::class.java).data
                data?.list?.let { list ->
                    if (page == 1) mList.clear()
                    mList.addAll(list)
                    mAdapter.notifyDataSetChanged()
                    mBinding.mCarRV.handlerPosition(page)
                }
            })
        } else {
            ApiMethods.apiSubscribe(ApiMethods.retrofit.partOutListV2(search, page), MyObserverHandlerLoading(this, loading, mBinding.mRefresh) {
                MyLog.d("零部件出库回调:$it")
                val data = getGson().fromJson(it, PartOutListEntity::class.java).data
                data?.list?.let { list ->
                    if (page == 1) mList.clear()
                    mList.addAll(list)
                    mAdapter.notifyDataSetChanged()
                    mBinding.mCarRV.handlerPosition(page)
                }
            })
        }


//        ApiMethods.apiSubscribe(ApiMethods.retrofit.partOutList(1, search, page), MyObserverHandlerLoading(this, loading, mBinding.mRefresh) {

    }

}